<template>
  <div class="eidt-name">
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="editUserList"
    />
    <van-field
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="7"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserList } from '@/api/users'
export default {
  neme: 'eidt-name',
  props: {
    value: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  methods: {
    async editUserList () {
      this.$toast.loading({
        message: '保存中....',
        forbidClick: true,
        duration: 0
      })
      try {
        const message = this.message
        if (!message.length) {
          this.$toast('昵称不能为空')
          return
        }
        await editUserList({
          name: this.value
        })

        this.$toast.success('修改昵称成功')
        this.$emit('input', message)
        this.$emit('close')
      } catch (err) {
        this.$toast('修改用户信息失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.eidt-name {
  .van-cell {
    margin-top: 10px;
  }
}
</style>
